const mysql =require('mysql');

const pool = mysql.createPool({
    host:'8.134.126.197',
    user:'root',
    password:'mydb',
    database:'mydb',
    port:3306
})
    /*
    "protocol" : "mysql",
    "host" : "8.134.126.197",
    "database" : "mydb",
    "user" : "root",
    "password" : "mydb",
    "port" : 3306
    */

const query = (sql,params) =>{
    return  new Promise((resolve, reject)=>{
        pool.getConnection((err,conn) =>{
            if(err){
                pool.releaseConnection();
                reject('连接mysql失败')
            }
            conn.query(sql,params,(err,result,fields)=>{
                if(err){
                    conn.release();//释放连接对象
                    reject('执行sql失败')
                }
                // callback({result, fields})
                resolve(result)
                conn.release();
            })
        })
    })

}
module.exports = query
